GBML manual / Motion

Motion and pathing

An instance can be moved by giving it a speed, by setting its position, or by handing it a path to follow - three different jobs with different rules.

Where things are, and what "moving" means

Every instance - one copy of an object placed in a room, such as one enemy or one coin - has a position: x and y, measured in pixels. The room's top-left corner is (0, 0): x counts pixels across from the room's left edge and y counts pixels down from its top edge (see Where (0, 0) is). x grows to the right. y grows downward, the way a screen counts, so a bigger y is lower on the screen.

Moving an instance means changing x and y. There are only a few ways to do it, and this page sorts them by what you are trying to get:

You wantUseWho does the movingDoes it stop at walls
A steady drift, a fall, a thrown objecta velocity: speed/direction or hsp/vspthe engine, every frame, by itselfno
A jump to an exact spot, or a one-off nudgex/y directly, or move_to, move_step, move_snap, move_wrapyou, once, when you call itno
Walking that respects wallsmove_slide, move_slide_solid, move_bounceyou, every frame you call ityes
Following a shape drawn in advancea path - see Pathsthe engine, along the pathno
Gliding to a spot over a set time, speeding up or braking on the waya tween - tween_to, see Tweensthe engine, until it arrivesno

Velocity: tell it how fast, and it keeps going

A frame is one picture of the game. The GBA shows 60 of them a second, and every speed on this page is measured in pixels per frame. A speed of 1 crosses the 240-pixel-wide screen in four seconds. A speed of 4 does it in one.

Give an instance a speed and it moves by that much on every frame from then on, with no further code. Two pairs of variables do this, and they are two views of the same motion:

You may write to either pair. The engine keeps the other pair in step, so reading speed after setting hsp gives a sensible answer. If you write to a pair this frame, that pair wins. Choose one pair for an instance and stay with it. direction comes from a table, so it is accurate to about a tenth of a degree, not exact.

// Step event of a character that strafes left and right
hsp = btn_dx() * 1.5;   // -1, 0 or 1 from the d-pad, times 1.5 pixels a frame

Two more variables act on a velocity for you:

The engine applies all of this in the Movement step of every frame, after your Step events. Set a speed in Step and it takes effect that same frame. See The order of a frame.

None of this looks at walls unless the instance lands on solids. An instance moving by velocity goes straight through anything solid, unless its Lands on solids box is ticked (or lands_on_solids is true): then it stops on solid tiles and solid objects, standing right on top of a floor with on_ground true, stopping under a ceiling, and sliding along a wall.

Placing: set the position yourself

Writing to x or y puts the instance there at once. Nothing is animated between the old spot and the new one. A few functions do common versions of this:

None of these check for walls either.

Colliding: moves that stop at walls

A function is a named action you call with values in brackets. Three of them move an instance and check the room's collision - its solid tiles and obstacles - while doing it:

Here is a top-down walker. It reads the d-pad as two numbers and hands them to move_slide, which stops it at walls and slides it along them:

// Step event of a top-down walker (hsp and vsp stay at 0)
move_slide(btn_dx() * 1.5, btn_dy() * 1.5);

Call these from Step, every frame. move_slide is given the distance directly, so leave hsp and vsp at 0 while you use it. Otherwise the automatic velocity movement also runs and moves the instance a second time. move_bounce is the opposite case: it works from hsp/vsp, which stay set, so the automatic movement also applies them and the instance covers about double the distance you might expect.

Pathing: the shape is decided in advance

Velocity answers "how fast and which way, right now." A path answers a different question: "follow this exact route." It is a list of points drawn once in the editor. An instance told to follow it goes point to point, and a patrolling guard, a moving platform or a conveyor of pickups needs no code beyond the start call. Paths covers what a path is and what happens when the instance reaches the end.

Two things to know about how the two systems meet:

Everything in this category

EntryWhat it is
hsp, vsphorizontal and vertical speed, pixels per frame
speed, directionspeed along an angle, in degrees
gravity, gravity_dirspeed added every frame, and which way
frictionspeed removed every frame, until 0
lands_on_solids, on_groundstop on solid tiles and objects, and whether it is standing on something
move_to, move_step, move_snap, move_wrapplacing, with no wall checks
move_slide, move_slide_solid, move_bouncemoving, with wall checks

solid and collides cover how the engine decides what counts as a wall. The functions that read a path are listed on the Paths page.

See also: PathsTweenshspspeedmove_slidepath_startThe order of a frame